function debug(message) {
    console.log(message);
}

$().ready(function() {

    $('.slider').mobilyslider({
   		content: '.sliderContent',
   		children: 'div',
   		transition: 'fade',
   		animationSpeed: 800,
   		autoplay: true,
   		autoplaySpeed: 15000,
   		pauseOnHover: false,
   		bullets: true,
   		arrows: true,
   		arrowsHide: true,
   		prev: 'prev',
   		next: 'next',
   		animationStart: function(){},
   		animationComplete: function(){}
   	});
    $('.registerit').hover(

            function(){hover1($(this).attr('class'))}, function(){hover2($(this).attr('class'))}

            );
    function test1(){alert(1);}
    function test2(){alert(2);}
   $('.arrows').toggle(firstF, secondF);


       function firstF(){
           var count=$('.podmenu:first').attr('id');

               $('.podmenu:first').addClass('deleteborderbottom');
               $('.podmenu:last').addClass('otherrubrics');

            if (count=='two')
           {
               $('.podmenu:first').addClass('deleteborderbottom');
               $('.podmenu:eq(1)').addClass('deleteborder');
               $('.podmenu:last').addClass('otherrubrics');

           }
           $('.arrows').addClass('aup').attr('title','Close');
           $('.novisible').toggle('slow');
       }
       function secondF(){

           $('.arrows').removeClass('aup').attr('title','More rubrics');
           $('.novisible').toggle('slow');
           $('.podmenu:first').removeClass('deleteborderbottom');
       }

    $('.managecomment').toggle(showComment, hiddenComment);

    function showComment(){
        $('.badcomment table').slideDown('slow');
        $('.badcomment hr').show();
        $('.addcomment').show();
        $(this).text('Скрыть комментарий');
    }
    function hiddenComment(){
        $('.addcomment:not(:first)').hide();
        $('.badcomment table').hide();
        $('.badcomment hr').hide();
        $(this).text('Показать комментарий');
    }
//    Font size controls
    $('#fontUp').click(function(e){
        e.preventDefault();
        fontSize(1);
    });
    $('#fontDown').click(function(e){
        e.preventDefault();
        fontSize(-1);
    });

    $('.comment-form').live('click', function() {
        var elem = this;
        var id = $(this).attr('id');
        var params = id.match(/comment_(\d+)_(\d*)/);
        var news = params[1];
        var parent = params[2];

        var form = $('#comment-form').clone();
        $('[name="parent"]', form).val(parent);
        $(form).show();
        $(elem).parent().html(form);

        return false;
    });

    $('.comment-add-form').live('submit', function() {
        var elem = this;
        var values = $(this).serialize();
        var news = $('[name="news"]',this).val();
        $.post('/comments/add', values, function (data) {
            if ('1' === data) {
                $.get('/comments/view', { news : news}, function(page) {
                    $('#comments').html(page);
                }, 'html');
            } else {
                $(elem).parent().html(data);
            }
        }, 'html');
        return false;
    });

    $('.comments').click(function() {
        var elem = this;
        var id = $(this).attr('id');
        var params = id.match(/comment_(\d+)/);
        var news = params[1];

        $.post('/comments/view', {
            'news' : news
        }, function(data) {
            $(elem).parent().html(data);
        }, 'html');
        return false;
    });

    $('#form-rubric').change(function() {
        var rubric = $(this).val();
        var selector = '#form-subrubric';
        $.post('/ajax/rubrics',
                {
                    'rubric' : rubric
                },
                function(data) {
                    $(selector).children().remove();
                    for (var key in data) {
                        $(selector).append('<option value="' + data[key]['id'] + '">' + data[key]['name'] + '</option>');
                    }
                }, 'json');
    });

    $("#query").searchStringWidget("/ajax/complete");

    $('.wrap').mouseover(function(){
        $('.source').stop().animate({opacity:1},500);
    });
    $('.wrap').mouseout(function(){
        $('.source').stop().animate({opacity:0},500);
    });

    initRating();
});

function initRating() {
    initNewsRationg();
    $('.auto-submit-star').rating({
        callback: function(value, link){
            $(this.form).submit();
        }
    });
}

function initNewsRationg() {
    var selector = "#news-rating";
    $(selector).submit(function() {
        var elem = this;
        $.post('/vote/vote', $(elem).serialize(), function (data) {
            $('input', elem).rating('readOnly', true)
        }, 'json');
        return false;
    });
}

var fontSize = function(bigger){
    bigger = bigger||1;

    var size = parseInt($('#article').attr('class').substring(4,6));
    if(size<18 && size>10){
        size = size+(bigger);
    }
    else{
        if(size==18 && bigger==-1)size=17;
        if(size==10 && bigger==1)size=11;
    }
    $('#article').attr('class', 'size'+size);
};

function initPoll() {
    $('#submit', '#form_poll').live('click', function() {
        $('#results', '#form_poll').replaceWith('<img id="results2" src="/images/spinner.gif"/>');
        var id = "";
        $(":checked").each(function () {
            id = this.id;

            $.post('/pollvotinghistory/ajaxindex', {
                    'id' : id
                },
                function(data) {
                    $('#results').parent().html(data);

                }, 'html');
        });
        $('#form_poll').toggle();
        return false;
    });
}

function initCommentsMark() {
    $('a.comment-rate').live('click', function() {
        var elem = this;
        var url = $(elem).attr('href');
        $.get(url, {}, function(data) {
            $(elem).parent().html(data);
        });
        return false;
    });
}

function initSubscribeValidate() {
    $('#subscribe-form').validate();
}

function initSubscribeForm() {
    $('#subscribe-form').live('submit',function() {
        var elem = this;
        var parent = $(elem).parent();
        $(parent).html(getSpinnerCode());
        var values = $(elem).serialize();
        $.post('/subscribe', values, function(data) {
            $(parent).html(data);
        }, 'html');
        return false;
    });
}

function getSpinnerCode() {
    return "<img src='/images/spinner.gif' alt='Loading'>";
}

function hover1(className){

    var newClass=className+"-hover";
    $("."+className).addClass(newClass);
    $("."+newClass).removeClass(className);

}
function hover2(className){

    var newClass=className.replace(('-hover'),'');
    $("."+className).addClass(newClass);
    $("."+newClass).removeClass(className);


}


